<%@ Page Language="C#"%>
<%@ Import namespace="SubSonic.Utilities"%>
<%@ Import Namespace="SubSonic" %>
<%@ Import Namespace="SubSonic.CodeGenerator" %>
<%@ Import Namespace="System.Data" %>

<%
    string providerName = "#PROVIDER#";
    ICodeLanguage language = new CSharpCodeLanguage();
	DataProvider provider = DataService.GetInstance(providerName);
    System.Collections.Generic.List<StoredProcedure> spList = new System.Collections.Generic.List<StoredProcedure>();
    System.Collections.Generic.List<StoredProcedure> allProcs = DataService.GetSPSchemaCollection(providerName);
    
    /* jeff_huntsman added: support for SP include/exclude lists */    
    // stored procedure include list 
    bool generateAll = false;
    if (provider.IncludeProcedures.Length == 1)
    {
        if (provider.IncludeProcedures[0] == "*")
            generateAll = true;
    }
    
    // stored procedure exclude list 
    if (generateAll)
    {
        foreach (StoredProcedure proc in allProcs)
        {
            bool exclude = false;
            foreach(string s in provider.ExcludeProcedures) 
            {
                if (Utility.IsMatch(s, proc.Name))
                {
                    exclude = true;
                    break;       
                }                    
            }
            if (!exclude)
            {
                // add the stored procedure to the generate list
                spList.Add(proc);
            }                
        }
    }
    else
    {
        foreach (StoredProcedure proc in allProcs)
        {            
            foreach (string s in provider.IncludeProcedures)
            {
                if (Utility.IsMatch(s, proc.Name))
                {
                    spList.Add(proc);
                    break;
                }
            }            
        }
    }
    
	ArrayList classStoredProcedures = new ArrayList();
	
%>
namespace <%=provider.GeneratedNamespace %>{

    public partial class <%=provider.SPClassName%>{
        <%foreach (StoredProcedure sp in spList)
{
    if(Utility.IsMatch(sp.TableName, provider.SPClassName))
    {        
        //load up a the params
        string argList = string.Empty;        
        foreach(StoredProcedure.Parameter p in sp.Parameters)
        {
            if(argList.Length > 0)
            {
                argList += ", ";
            }            
			// allow nullable types for SP parms - Thanks Jeff!
			string pType = Utility.GetVariableType(p.DBType, Utility.IsNullableDbType(p.DBType), language);
            string arg = p.DisplayName;
            argList += pType + " " + arg;            
        }
%>
        /// <summary>
        /// Creates an object wrapper for the <%=sp.Name%> Procedure        
        /// </summary>
        public static StoredProcedure <%=sp.DisplayName%>(<%=argList%>)
        {
            SubSonic.StoredProcedure sp = new SubSonic.StoredProcedure("<%=sp.Name%>" , DataService.GetInstance("<%= providerName %>"),"<%=sp.SchemaName%>");
        	<%
        foreach(StoredProcedure.Parameter p in sp.Parameters)
        {
            if(p.Mode == ParameterDirection.InputOutput)
               
            {%>
            sp.Command.AddOutputParameter("<%=p.QueryParameter%>",DbType.<%=Enum.GetName(typeof(DbType), p.DBType)%>);
        	    <%
            }
			else
            {%>
            sp.Command.AddParameter("<%=p.QueryParameter%>", <%=p.DisplayName%>,DbType.<%=Enum.GetName(typeof(DbType), p.DBType)%>);
        	    <%
            }
        }
%>
            return sp;
        }
        <%
    }
    else
    {
        classStoredProcedures.Add(sp);
    }
}
%>
    }
    <%
		foreach (StoredProcedure sp in classStoredProcedures)
		{
		    string className = sp.TableName;
		    TableSchema.Table tbl = DataService.GetTableSchema(sp.TableName, provider.Name, TableType.Table);
		    if(tbl != null)
		    {
		        className = tbl.ClassName;
		    }
		    string argList = string.Empty;		    
		    foreach(StoredProcedure.Parameter p in sp.Parameters)
		    {
		        if(argList.Length > 0)
		        {
		            argList += ", ";
		        }		        
				// allow nullable types for SP parms - Thanks Jeff!
				string pType = Utility.GetVariableType(p.DBType, Utility.IsNullableDbType(p.DBType), language);
		        string arg = p.DisplayName;
		        argList += pType + " " + arg;
		    }
%>
		public partial class <%=className%>
		{
		/// <summary>
        /// Creates an object wrapper for the <%=sp.Name%> Procedure
        /// </summary>
        public static StoredProcedure <%=sp.DisplayName%>(<%=argList%>){
		
            SubSonic.StoredProcedure sp = new SubSonic.StoredProcedure("<%=sp.Name%>" , DataService.GetInstance("<%= providerName %>"),"<%=sp.SchemaName%>");
        	<%
		    foreach(StoredProcedure.Parameter p in sp.Parameters)
		    {%>
        	    <%
		        if(p.Mode == ParameterDirection.InputOutput)
		        {%>
            sp.Command.AddOutputParameter("<%=p.QueryParameter%>",DbType.<%=Enum.GetName(typeof(DbType), p.DBType)%>);
        	    <%
		        }
		        else
		        {%>
            sp.Command.AddParameter("<%=p.QueryParameter%>", <%=p.DisplayName%>,DbType.<%=Enum.GetName(typeof(DbType), p.DBType)%>);
        	    <%
		        }%>
            <%
		    }%>
            return sp;
        }
		
		}
		<%
		}
%>
}
